package study.jqp.com.studydemo.utils;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import study.jqp.com.studydemo.R;

public class MenuAdapter extends BaseAdapter {

    private List<MenuItem> menuItems;
    private Context context;

    public MenuAdapter(List<MenuItem> menuItems, Context context) {
        this.menuItems = menuItems;
        this.context = context;
    }

    @Override
    public int getCount() {
        return menuItems.size();
    }

    @Override
    public MenuItem getItem(int i) {
        return menuItems.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View root = View.inflate(context, R.layout.item_main,null);
        ImageView iv_icon = root.findViewById(R.id.iv_icon);
        TextView tv_title = root.findViewById(R.id.tv_title);
        MenuItem item = getItem(i);
        iv_icon.setImageResource(item.getIcon());
        tv_title.setText(item.getTitle());
        root.setBackgroundResource(item.getBg());
        return root;
    }
}
